package xs.com.test;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import xs.com.mylibrary.allinone.ViewUtils;
import xs.com.mylibrary.allinone.view.annotation.ViewInject;


public abstract class BaseActivity extends AppCompatActivity {
    @ViewInject(R.id.toolbar)
    private Toolbar toolbar;
    @ViewInject(R.id.toolbar_title)
    protected TextView titleTv;
    protected Context mContext;
    protected TestApp app;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutRes());
        mContext = this;
        app = (TestApp) getApplication();
        ViewUtils.inject(this);
        initView();
        initData(savedInstanceState);
    }

    protected void initToolBar() {
        if (toolbar == null)
            toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("");
        if (titleTv == null)
            titleTv = (TextView) findViewById(R.id.toolbar_title);
        titleTv.setText("");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                leftClick();
            }
        });
    }

    protected void leftClick() {
        finish();
    }

    protected void initView() {
        initToolBar();
    }

    protected abstract void initData(Bundle savedInstanceState);

    protected abstract int getLayoutRes();

}
